package com.lezheba.action;

import java.io.IOException;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.util.JSONBuilder;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.lezheba.db.entity.Category;
import com.lezheba.db.entity.Deal;
import com.lezheba.service.IndexService;

public class IndexAction extends BaseAction {
	private static Logger log = Logger.getLogger(IndexAction.class);

	private static final long serialVersionUID = 3828494970421292037L;

	private static  String cityName = "全国";
	private String cityId;

	public String getCityId() {
		return cityId;
	}

	public void setCityId(String cityId) {
		this.cityId = cityId;
	}

	private long cateId;
	private String key;

	private int page = 1;
	private int limit = 15; // page number
	private int total;

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public long getCateId() {
		return cateId;
	}

	public void setCateId(long cateId) {
		this.cateId = cateId;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = super.getAfterTranslate(key);
		// this.key = key;
	}

	public void setDealList(List<Deal> dealList) {
		this.dealList = dealList;
	}

	private List<Deal> dealList;
	private TreeMap cityMap;

	private String email;
	private String password;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String login() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.reset();
		response.setContentType("text/html;charset=utf-8");
		IndexService service = new IndexService();

		JSONBuilder builder = new JSONBuilder(response.getWriter());

		builder.object();
		if(service.checkUser(email, password)){
			builder.key("tag").value(TAG_SUCCESS);
			super.putSession("email", email);
		}else{
			builder.key("tag").value(TAG_FAILED);
		}
		builder.key("email").value(email);
		builder.endObject();
		response.getWriter().close();
		return SUCCESS;
	}
	
	public String register() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.reset();
		response.setContentType("text/html;charset=utf-8");
		IndexService service = new IndexService();

		JSONBuilder builder = new JSONBuilder(response.getWriter());

		builder.object();
		if(!service.isExsitUser(email)){			
			service.saveUser(email, password);
			builder.key("tag").value(TAG_SUCCESS);
			super.putSession("email", email);
		}else{
			builder.key("tag").value(TAG_FAILED);
		}
		builder.key("email").value(email);
		builder.endObject();
		response.getWriter().close();
		return SUCCESS;
	}
	
	public String logout() throws IOException {		
		super.putSession("email", "");
		return SUCCESS;
	}

	public String execute() {
		IndexService service = new IndexService();
		cityMap = service.getCityMap();
		return SUCCESS;
	}

	public String getDeals() {

		IndexService service = new IndexService();
		if (cityName == null || cityName.length() < 2) {
			cityName = "";
		}

		if (cityName.endsWith("市")) {
			cityName = cityName.substring(0, cityName.length() - 1);
		}
		total = ((Long) service.getDealtotal(cityName, cateId, key)).intValue();
		dealList = service.getDealList(cityName, cateId, key, page, limit);

		return SUCCESS;
	}

	public List<Deal> getDealList() {
		return dealList;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = super.getAfterTranslate(cityName);
		// cityName;
	}

	public TreeMap getCityMap() {
		return cityMap;
	}

}
